package com.hue.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/*
    微信服务
 */
@Controller
@CrossOrigin
@RequestMapping("/wx")
public class WxController {
    @Value("${wx.open.appid}")
    private String appid;

    @Value("${wx.open.appsecret}")
    private String appsecret;

    @Value("${wx.open.redirecturl}")
    private String redirecturl;

    /*
        生成二维码
     */
    @GetMapping("/qrCode")
    public String qrCode() {
        // 微信开放平台授权baseUrl  %s相当于?代表占位符
        String url = "https://open.weixin.qq.com/connect/qrconnect" +
                "?appid=%s" +
                "&redirect_uri=%s" +
                "&response_type=code" +
                "&scope=snsapi_login" +
                "&state=%s" +
                "#wechat_redirect";
        try {
            //对redirecturl进行url编码
            String encodeurl = URLEncoder.encode(redirecturl, "UTF-8");
            //拼接url
            url = String.format(url,appid,encodeurl,"hue");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //请求微信生成二维码的地址
        return "redirect:"+url;
    }
}
